package com.maysham.coder.server.controller;

import com.maysham.coder.server.common.Response;
import com.maysham.coder.server.config.auth.SecurityUtil;
import com.maysham.coder.server.dto.UserDto;
import com.maysham.coder.server.model.Group;
import com.maysham.coder.server.model.User;
import com.maysham.coder.server.service.GroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/api/token", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class TokenController {
    @Resource
    private GroupService groupService;

    @GetMapping("/user")
    public Response<UserDto> checkToken() {
        User user = SecurityUtil.current();
        UserDto data = new UserDto();
        BeanUtils.copyProperties(user, data);
        List<Group> groups = groupService.queryUserGroups(user.getId());
        data.setGroups(groups);
        return Response.ok(data);
    }
}
